<?php
/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */

namespace app\admin\controller;
use think\facade\View;

use app\common\logic\ModuleLogic;
use think\facade\Db;
use think\facade\Cache;

use app\admin\validate\AdminRole as validateAdminRole;

use app\common\model\SystemMenu as SystemMenuModel;
use app\common\model\AdminRole as AdminRoleModel;
use app\common\model\Admin as AdminModel;

class Role extends Base
{
    function list(){
        if(IS_AJAX){

            $model = new AdminRoleModel();
            $params = input('param.');

            $res = $model->tableData($params);
            return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);

        }else {
            return View::fetch();
        }
    }

    public function add(){
        if(IS_POST){
            $param = input('param.');

            $validate = new validateAdminRole;
            $result = $validate->scene('add')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }

            $AdminRoleModel = new AdminRoleModel();
            $res = $AdminRoleModel -> handle($param);

            return dyajaxReturn($res['code'],$res['msg'],$param);
        }else {

            $right_list = Db::name('system_menu')->order('sort asc')->select()->toArray();
            foreach($right_list as $k=>$v){
                $right_list[$k]['show_name'] = $v['show'] == 1 ?'显示':'不显示';
            }
            $SystemMenuModel = new SystemMenuModel();
            $right_list = $SystemMenuModel->menulist($right_list,0,0);

            View::assign('right_list',$right_list);

            return View::fetch();
        }
    }


    public function edit(){
        $type = input('type',0);
        $moduleLogic = new ModuleLogic;
        
        if (!$moduleLogic->isModuleExist($type)) {
            $this->error('模块不存在或不可见');
        }
        
        if(IS_POST){
            $param = input('param.');

            $validate = new validateAdminRole;
            $result = $validate->scene('add')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }

            $AdminRoleModel = new AdminRoleModel();
            $res = $AdminRoleModel -> handle($param);

            return dyajaxReturn($res['code'],$res['msg'],$param);
        }else{
            $param = input('param.');
            $role_id = $param['role_id'];
            $AdminRoleModel = new AdminRoleModel();
            $info = $AdminRoleModel -> where('role_id','=',$role_id)->find();
            $info['act_list_arr'] = explode(',',$info['act_list']);

            $right_list = Db::name('system_menu')->order('sort asc')->select()->toArray();
            foreach($right_list as $k=>$v){
                $right_list[$k]['show_name'] = $v['show'] == 1 ?'显示':'不显示';
            }
            $SystemMenuModel = new SystemMenuModel();
            $right_list = $SystemMenuModel->menulist($right_list,0,0);

            View::assign('right_list',$right_list);
            View::assign('role_id',$role_id);
            View::assign('info',$info);

            return View::fetch();
        }
    }
     
     public function delete(){
     	$role_id = input('role_id');
     	if(!empty($role_id)){
     	    $AdminModel = new AdminModel();
            $admin_info = $AdminModel -> where('role_id','=',$role_id)->find();
            if($admin_info){
                return dyajaxReturn(0,'角色下有管理员不可删除');
            }
            $AdminRoleModel = new AdminRoleModel();

     		$r = $AdminRoleModel->where('role_id','=',$role_id)->delete();
     		if($r){
     		    adminLog("角色管理删除",$role_id);
                return dyajaxReturn(1,'删除成功');
     		}else{
                return dyajaxReturn(0,'删除失败');
     		}
     	}else{
            return dyajaxReturn(0,'参数有误');
     	}
     }


     //获取它所有子栏目
    public function getChilds(){
        $id = input('param.id');

        $right_list = Db::name('system_menu')->order('sort asc')->select()->toArray();

        $SystemMenuModel = new SystemMenuModel();
        $ids = $SystemMenuModel->getAllChilds($right_list,$id);

        return dyajaxReturn(0,'参数有误',$ids);
    }

    //关闭开启
    public function changeStatus(){
        $param = input('param.');
        $role_id = $param['role_id'];
        $checked = $param['checked'];

        $model = new AdminRoleModel();
        $data['update_time'] = time();
        if($checked == 'true'){
            $data['is_open'] = 1;
            $res = $model -> where('role_id','=',$role_id)->update(['is_open'=>1]);
            $ty = '开启';
        }else{
            $data['is_open'] = 0;
            $res = $model -> where('role_id','=',$role_id)->update(['is_open'=>0]);
            $ty = '关闭';
        }
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        adminLog("角色管理".$ty,$role_id);
        return dyajaxReturn(1,'操作成功');
    }


}